//
//  CRRegisterController.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRRegisterController.h"
#import "CRRegisterModel.h"
#import "CRRegisterView.h"
#import "CRUserManager.h"
@interface CRRegisterController ()
@property (nonatomic, strong) CRRegisterView *registerView;
@property (nonatomic, strong) CRRegisterModel *registerModel;

@property (nonatomic, strong) CRUserManager *manager;
@end

@implementation CRRegisterController

- (CRUserManager *)manager {
    if (!_manager) {
        _manager = [CRUserManager new];
    }
    return _manager;
}
- (void)loadView
{
    [super loadView];
    self.registerView = [[CRRegisterView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.registerView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutView];
    [self layoutData];
}
/**
 *  界面展示
 */
- (void)layoutView
{
    self.registerModel = [CRRegisterModel new];
    [self.registerView.cancleButton addTarget:self action:@selector(clickCancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView.registerButton addTarget:self action:@selector(clickRegisterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView.getCodeButton addTarget:self action:@selector(clickGetCodeButton:) forControlEvents:UIControlEventTouchUpInside];
}
/**
 *  数据
*/
- (void)layoutData
{
}

#pragma mark >>>> 按钮监听事件
- (void)clickGetCodeButton:(UIButton *)button {
//    [self.manager verifyWithMobilePhone:self.registerView.emailText.text success:^{
//        NSLog(@"验证成功");
//    } failure:^(NSError *error) {
//        NSLog(@"验证失败 %@ ", error);
//    }];
    
    [self.manager registerWithUsername:self.registerView.userText.text password:self.registerView.passwordText.text phone:self.registerView.emailText.text];
}

//点击取消按钮事件
- (void)clickCancleButtonAction:(UIButton *)button
{
    //模态退出
    [self dismissViewControllerAnimated:YES completion:nil];
}
//点击注册按钮事件
- (void)clickRegisterButtonAction:(UIButton *)button
{
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    NSString *userText = self.registerView.userText.text;
    NSString *passwordText = self.registerView.passwordText.text;
    NSString *affirmPWText = self.registerView.passwordText.text;

    
    if ([userText isEqualToString:@""] || [passwordText isEqualToString:@""] || [affirmPWText isEqualToString:@""] || [self.registerView.emailText.text isEqualToString:@""]){
       
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"请检查，是或否填写完整" preferredStyle:UIAlertControllerStyleAlert];
//        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];

      
    }else if (![passwordText isEqualToString:affirmPWText]){
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"密码有误，请核对再试" preferredStyle:UIAlertControllerStyleAlert];
//        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }else{
        // 向服务器注册用户
//        [self.manager registerWithUsername:self.registerView.userText.text password:self.registerView.passwordText.text email:self.registerView.emailText.text success:^{
//            NSLog(@"注册成功");
//            [self dismissViewControllerAnimated:YES completion:nil];
//        } failure:^(NSError *error) {
//            NSLog(@"注册失败%@", error);
//        }];
        
        [self.manager verifyWithMobilePhone:self.registerView.codeText.text success:^{
            NSLog(@"注册成功");
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(NSError *error) {
            NSLog(@"注册失败%@", error);
        }];
        
        
        self.registerModel = self.registerView.registerModel;
        
//        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
////        [alertVC addAction:cancelAction];
//        [alertVC addAction:sureAction];

//        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
